package com.rk.hosp.controller.api;
import com.alibaba.excel.util.StringUtils;
import com.rk.common.helper.HttpRequestHelper;
import com.rk.common.result.Result;
import com.rk.hosp.aop.SystemCheckSign;
import com.rk.hosp.model.hosp.Department;
import com.rk.hosp.model.hosp.Hospital;
import com.rk.hosp.model.hosp.Schedule;
import com.rk.hosp.service.DepartmentService;
import com.rk.hosp.service.HospitalService;
import com.rk.hosp.service.ScheduleService;
import com.rk.hosp.vo.hosp.DepartmentQueryVo;
import com.rk.hosp.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author :Rk.
 * @date : 2022/11/9
 */
@Slf4j
@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {


    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    /**
     * 保存医院信息
     * @param request
     * @return
     */
    @ApiOperation(value = "保存医院信息")
    @PostMapping("/saveHospital")
    @SystemCheckSign
    public Result saveHosp(HttpServletRequest request){
        log.info("------------开始保存医院信息---------");
        //获取传递过来的医院的信息
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        //base64传输过程中需要纠正
        //传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoDataString = (String)paramMap.get("logoData");
        if(!StringUtils.isEmpty(logoDataString)) {
            String logoData = logoDataString.replaceAll(" ", "+");
            paramMap.put("logoData", logoData);
        }

        try {
            //调用service方法存入mongodb
            hospitalService.saveOrUpdate(paramMap);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("上传医院信息失败");
        }

        return Result.ok();
    }

    /**
     * 保存医院信息
     * @param request
     * @return
     */
    @ApiOperation(value = "批量保存医院信息")
    @PostMapping("/saveBatchHospital")
    public Result saveBatchHospital(HttpServletRequest request){
        log.info("------------开始批量保存医院信息---------");
        //获取传递过来的医院的信息
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        //base64传输过程中需要纠正
        //传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoDataString = (String)paramMap.get("logoData");
        if(!StringUtils.isEmpty(logoDataString)) {
            String logoData = logoDataString.replaceAll(" ", "+");
            paramMap.put("logoData", logoData);
        }

        try {
            //调用service方法存入mongodb
            hospitalService.saveOrUpdate(paramMap);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("上传医院信息失败");
        }

        return Result.ok();
    }

    /**
     * 查询医院信息
     * @param request
     * @return
     */
    @ApiOperation(value = "获取医院信息")
    @PostMapping("/hospital/show")
    @SystemCheckSign //校验签名
    public Result hospital(HttpServletRequest request) {
        log.info("------------开始获取医院信息---------");
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String)paramMap.get("hoscode");
        Hospital hospital = null;
        try {
            hospital = hospitalService.getByHoscode(hoscode);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("查询医院信息失败!");
        }
        return Result.ok(hospital);
    }

    /**
     * 保存科室信息
     * @param request
     * @return
     */
    @ApiOperation(value = "上传科室信息")
    @PostMapping("/saveDepartment")
    @SystemCheckSign
    public Result saveDepartment(HttpServletRequest request){
        log.info("------------上传保存科室信息-----------");
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //调用service方法存入mongodb
        try {
            departmentService.saveOrUpdate(paramMap);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("上传科室信息失败!");
        }
        return Result.ok();
    }


    /**
     * 查询科室信息
     * @param request
     * @return
     */
    @ApiOperation(value = "获取科室信息分页列表")
    @PostMapping("/department/list")
    @SystemCheckSign
    public Result findAllDepartment(HttpServletRequest request){
        log.info("------------开始获取科室信息分页列表-----------");
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String)paramMap.get("hoscode");

        //非必填
        String depcode = (String)paramMap.get("depcode");
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String)paramMap.get("limit"));

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        departmentQueryVo.setDepcode(depcode);
        Page<Department> pageModel = null;
        try {
            pageModel = departmentService.selectPage(page, limit, departmentQueryVo);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("查询科室信息失败!");
        }

        //校验通过后 分页查询科室信息
        return Result.ok(pageModel);
    }



    @ApiOperation(value = "删除科室信息")
    @PostMapping("/department/remove")
    @SystemCheckSign
    public Result removeDepartment(HttpServletRequest request){
        log.info("------------开始删除科室信息-----------");

        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String)paramMap.get("hoscode");
        String depcode = (String)paramMap.get("depcode");
        try {
            departmentService.removeDepartment(hoscode,depcode);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("删除失败!");
        }
        log.info("------------科室信息已删除-----------");
        return Result.ok();
    }


    /**
     * 上传排班信息
     * @param request
     * @return
     */
    @ApiOperation(value = "上传排班信息")
    @PostMapping("/saveSchedule")
    @SystemCheckSign
    public Result saveSchedule(HttpServletRequest request){
        log.info("------------开始上传排班信息-----------");
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //调用service方法存入mongodb
        try {
            scheduleService.saveOrUpdate(paramMap);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("上传失败");
        }
        return Result.ok();
    }


    /**
     * 查询排班信息
     * @param request
     * @return
     */
    @ApiOperation(value = "获取排班信息分页列表")
    @PostMapping("/schedule/list")
    @SystemCheckSign
    public Result findAllSchedule(HttpServletRequest request){
        log.info("------------开始获取排班信息分页列表-----------");
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String)paramMap.get("hoscode");

        //非必填
        String depcode = (String)paramMap.get("depcode");
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String)paramMap.get("limit"));

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);
        Page<Schedule> pageModel = null;
        try {
            pageModel = scheduleService.selectPage(page, limit,scheduleQueryVo);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("查询排班信息失败!");
        }

        //校验通过后 分页查询科室信息
        return Result.ok(pageModel);
    }



    @ApiOperation(value = "删除排班信息")
    @PostMapping("/schedule/remove")
    @SystemCheckSign
    public Result removeSchedule(HttpServletRequest request){
        log.info("------------开始删除排班信息-----------");

        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String)paramMap.get("hoscode");
        String hosScheduleId = (String)paramMap.get("hosScheduleId");
        try {
            scheduleService.removeSchedule(hoscode,hosScheduleId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("删除失败!");
        }
        log.info("------------排班信息已删除-----------");
        return Result.ok();
    }


}
